package com.xty.xtyle.base;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xty.xtyle.R;
import com.xty.xtyle.db.ClothTypesDao;
import com.xty.xtyle.db.cloth.Cloth;
import com.xty.xtyle.db.cloth.ClothDao;
import com.xty.xtyle.detail.ClothDetailActivity;
import com.xty.xtyle.event.FreshClothEvent;
import com.xty.xtyle.utils.CommonUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.List;


public class TabFragment extends AbsFragment {
    private RecyclerView recyclerView;
    private View cloth_empty;
    List<Cloth> cloths;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_tab;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        recyclerView = findViewById(R.id.rv);
        cloth_empty = findViewById(R.id.cloth_empty);
        Bundle bundle = getArguments();
        String type = bundle.getString("type");

        freshDataFromEvent();

    }

    void freshDataFromEvent() {
        Bundle bundle = getArguments();
        String type = bundle.getString("type");

        ClothDao dao = new ClothDao();
        cloths = dao.findAllByTypes(type);
        //查询该类型的所有衣服,空显示没有衣服
        if (null == cloths || cloths.isEmpty()) {
            showEmpty(true);
        }else {
            showEmpty(false);
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
            recyclerView.setAdapter(new StringAdapter());
        }
    }

    @Override
    public void freshData() {

    }

    @Subscribe
    public void onEvent(FreshClothEvent result) {
        //接收以及处理数据
        freshDataFromEvent();
    };

    private void showEmpty(boolean show) {
        if (show) {
            cloth_empty.setVisibility(View.VISIBLE);
        }else {
            cloth_empty.setVisibility(View.GONE);
        }
    }

    private class StringAdapter extends RecyclerView.Adapter<StringAdapter.VH> {

        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            return new VH(LayoutInflater.from(parent.getContext()).inflate( R.layout.item, parent, false));
        }

        //显示衣服信息
        @Override
        public void onBindViewHolder(@NonNull VH holder, int position) {
            final Cloth cloth = cloths.get(position);
            holder.textView.setText(cloth.getType()); //类型
//            if (!TextUtils.isEmpty(cloth.getImage())) {
//                File file = new File(cloth.getImage());
//                if (null != file && file.exists()) {
//                    CommonUtil.showToast("存在");
//                }else {
//                    CommonUtil.showToast("不存在");
//                }
//            }

            Glide.with(holder.itemView).load(cloth.getImage()).into(holder.ivCover);//图片
            //点击进入衣服详情
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ClothDetailActivity.startActivity(view.getContext(), cloth.getId());
                }
            });
        }

        @Override
        public int getItemCount() {
            return cloths.size();
        }

        class VH extends RecyclerView.ViewHolder {
            TextView textView;
            ImageView ivCover;
            public VH(@NonNull View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_title);
                ivCover = itemView.findViewById(R.id.iv_cover);
            }
        }
    }

}
